/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package net.sf.l2j.gameserver.custom.events.engine;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.l2j.Config;
import net.sf.l2j.gameserver.ThreadPoolManager;
import net.sf.l2j.gameserver.custom.events.DeathmatchEvent;

/**
 * @author Anarchy
 *
 */
public class EventManager
{
	protected IEvent _activeEvent = null,
		_activeGlobalEvent = null;
	private List<IEvent> _events;
	public Map<IEvent, Integer> _eventsTasks;
	
	public static EventManager getInstance()
	{
		return SingletonHolder._instance;
	}
	
	protected EventManager()
	{
		_eventsTasks = new HashMap<>();
		_events = new ArrayList<>();
		
		if (Config.ALLOW_DEATHMATCH_EVENT)
			_events.add(new DeathmatchEvent());
		
		for (IEvent e : _events)
		{
			startAllTasks(e);
		}
	}
	
	public void startAllTasks(final IEvent e)
	{
		int i = 0;
		Calendar cld = Calendar.getInstance();
		
		for (String s : e.getTimes().split(","))
		{
			String[] ss = s.split(":");
			int h = Integer.parseInt(ss[0]),
				m = Integer.parseInt(ss[1]);
			
			cld.set(Calendar.HOUR_OF_DAY, h);
			cld.set(Calendar.MINUTE, m);
			cld.set(Calendar.SECOND, 0);
			
			if (cld.getTimeInMillis()-System.currentTimeMillis() < 0)
			{
				cld.set(Calendar.DAY_OF_MONTH, cld.get(Calendar.DAY_OF_MONTH)+1);
				cld.set(Calendar.HOUR_OF_DAY, h);
				cld.set(Calendar.MINUTE, m);
				cld.set(Calendar.SECOND, 0);
			}
			
			if (cld.getTimeInMillis()-System.currentTimeMillis()  > 0)
			{
				i++;
				
				if (_eventsTasks.containsKey(e))
				{
					_eventsTasks.remove(e);
					_eventsTasks.put(e, i);
				}
				else
				{
					_eventsTasks.put(e, i);
				}
				
				ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
				{
					@Override
					public void run()
					{
						if (e.getType() != EventType.GLOBAL)
							_activeEvent = e;
						else
							_activeGlobalEvent = e;
						
						if (_eventsTasks.get(e) == 1)
						{
							_eventsTasks.remove(e);
							startAllTasks(e);
						}
						else
						{
							int t = _eventsTasks.get(e);
							_eventsTasks.remove(e);
							_eventsTasks.put(e, t--);
						}
						e.runEvent();
						
						_activeEvent = null;
					}
				}, cld.getTimeInMillis()-System.currentTimeMillis());
			}
		}
	}
	
	public IEvent getActiveEvent()
	{
		return _activeEvent;
	}
	
	public IEvent getActiveGlobalEvent()
	{
		return _activeGlobalEvent;
	}
	
	private static class SingletonHolder
	{
		protected static final EventManager _instance = new EventManager();
	}
}
